/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦，本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_console.htm
 *
 * Zhiqim Console is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager;

import org.zhiqim.httpd.HttpSessionUser;
import org.zhiqim.httpd.context.ZmlContextConstants;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.manager.dbo.ZmrOperator;

/**
 * 操作员会话用户信息
 *
 * @version v1.6.0 @author zouzhigang 2021-3-21 新建与整理
 */
@AnAlias("ZmrSessionUser")
public class ZmrSessionUser extends HttpSessionUser implements ZmlContextConstants, ZmrConstants
{
    private ZmrOperator operator;
    
    //两个iFrame内属性，当前主页和是否边展开导航
    private String mainUrl;
    private boolean sidebar = true;
    
    public ZmrSessionUser(ZmrOperator operator)
    {
        this.operator = operator;
    }
    
    @Override
    public String getSessionName()
    {
        return "超级管理员";
    }
    
    /**************************************************************************************/
    //修改会话属性，有可能需要更新到远程会话管理器
    /**************************************************************************************/
    
    public void setOperator(ZmrOperator operator)
    {
        this.operator = operator;
        this.flush();
    }
    
    /**************************************************************************************/
    //获取属性
    /**************************************************************************************/
    
    public ZmrOperator getOperator()
    {
        return operator;
    }
    
    public String getOperatorCode()
    {
        return operator.getOperatorCode();
    }
    
    public String getRootPath(String path)
    {
        return getContext().getRootPath(path);
    }
    
    /**************************************************************************************/
    //iFrame中的主页和边导航相关信息，不flush到可能的远程会话管理中
    /**************************************************************************************/
    
    public void setMainUrl(String mainUrl)
    {
        this.mainUrl = mainUrl;
    }
    
    public void setSidebar(boolean sidebar)
    {
        this.sidebar = sidebar;
    }

    public boolean hasSidebar()
    {
        return sidebar;
    }

    public String getSidebarClass()
    {
        return sidebar?"":"z-hide";
    }

    public String showSidebarMenu(String name, String ico, String url)
    {
        if (Validates.isEmptyBlank(name) || Validates.isEmptyBlank(ico) || Validates.isEmptyBlank(url))
            return _EMPTY_;
        
        if (!url.startsWith("/"))
            url = "/"+getContext().getAttribute(ZMR_PATH)+"/"+url;
        url = getRootPath(url);
        StringBuilder strb = new StringBuilder();
        strb.append("<li onclick=\"Zin.doClickChildMenu(this, '").append(url).append("');\"");
        
        if (mainUrl.equals(url))
        {
            strb.append(" class=\"active\" data-url=\"").append(url).append("\"");
        }
        strb.append(">");
        strb.append("<i class=\"z-font z-mg-r10 ").append(ico).append("\"></i>").append(name).append("</li>");
        return strb.toString();
    }
    
    /**************************************************************************************/
    //头像相关信息
    /**************************************************************************************/
    
    public String getAvatar50()
    {
        return operator.getOperatorAvatar()==0?getRootPath(_PATH_SERVICE_RES_AVATAR_):getAvatarUrl(getOperatorAvatar(), 50);
    }

    public String getAvatar100()
    {
        return operator.getOperatorAvatar()==0?getRootPath(_PATH_SERVICE_RES_AVATAR_):getAvatarUrl(getOperatorAvatar(), 100);
    }
    
    public String getAvatar150()
    {
        return operator.getOperatorAvatar()==0?getRootPath(_PATH_SERVICE_RES_AVATAR_):getAvatarUrl(getOperatorAvatar(), 150);
    }
    
    public long getOperatorAvatar()
    {
        return operator.getOperatorAvatar();
    }
    
    public String getAvatarUrl(long avatarId, int avatarSize)
    {
        return getRootPath(new StringBuffer("/").append(getContext().getAttribute(ZMR_PATH)).append("/avatar/").append(avatarId).append("/").append(avatarSize).append(".png").toString());
    }
}
